package com.maoye.ossp.biz.brand.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.db.api.IdGenerator;
import com.maoye.ossp.biz.brand.entity.BrandCate;
import com.maoye.ossp.biz.brand.entity.BrandStyle;
import com.maoye.ossp.biz.brand.service.IBrandCateService;
import com.maoye.ossp.biz.brand.service.IBrandLevelService;
import com.maoye.ossp.biz.brand.service.IBrandService;
import com.maoye.ossp.biz.brand.service.IBrandStyleService;
import com.maoye.ossp.biz.brand.utils.BrandCommonConstant;
import com.maoye.ossp.biz.brand.utils.BrandUtils;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysDictDetail;
import com.maoye.ossp.sys.base.utils.SysDictUtils;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.sc.api.domain.User;
import com.maoye.sc.service.user.UserServiceImpl;

/**
 * 对象功能:品牌品类关系表 控制器类
 * 开发公司:maoye shenzhen
 * 开发人员:harry
 * 创建时间:2013-09-30 11:23:41
 */
@Controller
@RequestMapping("/biz/brand/brandcate/")
public class BrandCateController extends BaseController
{
	@Resource
	private IBrandCateService brandCateService;
	@Resource
	private IBrandLevelService brandLevelService;
	@Resource
	private IBrandService brandService;
	@Resource 
	private IBrandStyleService brandStyleService;
	@Resource
	public IdGenerator idGenerator;
	@Resource
	public UserServiceImpl userServiceImpl;
	/**
	 * 添加或更新品牌品类信息
	 * @param brandLevel
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "save")
	public String save(BrandCate brandCate, Model model,HttpServletRequest request) 
	{
		String navId = request.getParameter("navId");
		String resultPage = resultPage("redirect:/biz/brand/brandcate/list?navId="+navId	, model, request);
		try
		{
			String msg = brandCateService.saveData(brandCate);
			
			if(null != msg && !"".equals(msg))
			{
				addMessage(model, msg);
				model.addAttribute("brandCate", brandCate);
				resultPage = "/biz/brand/brandcate/apply";
			}
		}
		catch(Exception e)
		{
			addMessage(model, BrandCommonConstant.EX_1009);
			resultPage =  "/biz/brand/brandcate/apply";
		}
		return resultPage;
	}
	
	@RequestMapping(value = "submit")
	public String submit( Model model,BrandCate brandCate,RedirectAttributes redirectAttributes,HttpServletRequest request,HttpServletResponse response) throws Exception {
		//Supplier getsupplier = supplierService.get(supplier.getId());
		brandCateService.submit(brandCate);
		addMessage(redirectAttributes, "提交'" + brandCate.getBrand().getBrandCname()+ "品牌'成功");
		String navId = request.getParameter("navId");
		return "redirect:/biz/brand/brandcate/list?navId="+navId;
	}
	
	/**
	 * 取得品牌品类信息分页列表
	 * @param brand
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "list")
	public String list(BrandCate brandCate, HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception
	{
		QueryFilter queryFilter = getQuerFilter(request, brandCate);
		User user = userServiceImpl.getUserByAccountID(UserUtils.getCurrentAccountId().toString());
		if(null != user && "SUPPLIER".equals(user.getUserType()))
		{
			queryFilter.getParams().put("createBy", user.getId());
		}
		Page<BrandCate> page = brandCateService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/brand/brandcate/list", model, request);
	}
	@RequestMapping(value = "dialoglist")
	public String dialoglist(BrandCate brandCate, HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception
	{
		QueryFilter queryFilter = getQuerFilter(request, brandCate);
		Page<BrandCate> page = brandCateService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/brand/brandcate/modalBrandList", model, request);
	}
	
	@RequestMapping(value = "add")
	public String add(BrandCate brandCate, Model model,HttpServletRequest request) throws Exception 
	{
		//TODO
		//根据cateID 联动：品牌风格、品牌价格带。判断为配套类：展示客单价、月消费频次、品牌知名度、
		//其它基本工程要求。
		//Long cateId = brandCate.getCateId();
		brandCate.setViceId(idGenerator.getUId());
		String navId = request.getParameter("navId");
		model.addAttribute("navId", navId);
		model.addAttribute("brandCate", brandCate);
		return "/biz/brand/brandcate/apply";
	}
	
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model,HttpServletRequest request) throws Exception 
	{
		BrandCate brandCate = brandCateService.getById(Long.valueOf(id));
		String navId = request.getParameter("navId");
		model.addAttribute("navId", navId);
		model.addAttribute("brandCate", brandCate);
		return "/biz/brand/brandcate/apply";
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id, Model model,HttpServletRequest request) 
	{
		BrandCate brandCate = brandCateService.getById(Long.valueOf(id));
		setDictToModel(model);
		String navId = request.getParameter("navId");
		model.addAttribute("navId", navId);
		model.addAttribute("brandCate", brandCate);
		return "/biz/brand/brandcate/view";
	}

	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes,HttpServletRequest request) 
	{
		Long[] lAryId = {Long.valueOf(id)};
		try {
			brandCateService.delAll(lAryId);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		String navId = request.getParameter("navId");
		addMessage(redirectAttributes, BrandCommonConstant.MSG_2006);
		return "redirect:/biz/brand/brandcate/list?navId="+navId;
	}
	
	@RequestMapping(value = "deleteAll")
	public String deleteAll(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) 
	{
		String deleteIds = request.getParameter("deleteIds");
		Long[] lAryId = BrandUtils.getLongAryByStr(deleteIds,";");
		try {
			brandCateService.delAll(lAryId);
			addMessage(redirectAttributes, BrandCommonConstant.MSG_2006);
		} catch (Exception e) {
			e.printStackTrace();
			addMessage(redirectAttributes, BrandCommonConstant.EX_1012);
		}
		String navId = request.getParameter("navId");
		return "redirect:/biz/brand/brandcate/list?navId="+navId;
	}
	
	@RequestMapping(value = "brandInfoList")
	public String listToContract(BrandCate brandCate, HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception
	{
		//List<BrandCate> brandC=brandCateService.getBySupplierNo("10001727");
		List<BrandCate> brandCateList=brandCateService.getBaseInfoList();
		model.addAttribute("brandCateList", brandCateList);
		return resultPage("/biz/brand/brandcate/list", model, request);
	}
	
	public void setDictToModel(Model model){
		//List<SysDictDetail> dictStyleList = SysDictUtils.getSysDictDetailList("brandStyle");
		List<BrandStyle> styleList = brandStyleService.getBySZCateId(4630000000000006L);
		List<SysDictDetail> dictIncomeLevelList = SysDictUtils.getSysDictDetailList(BrandCommonConstant.DICT_BRAND_INCOME_LEVEL);
		List<SysDictDetail> dictAgeLevelList = SysDictUtils.getSysDictDetailList(BrandCommonConstant.DICT_BRAND_AGE_LEVEL);
		model.addAttribute("styleList", styleList);
		model.addAttribute("dictIncomeLevelList", dictIncomeLevelList);
		model.addAttribute("dictAgeLevelList", dictAgeLevelList);
	}
	
	@RequestMapping(value = "{id}/sap")
	public String sendSap(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request) {
		BrandCate brandCate = brandCateService.get(Long.valueOf(id));
		String msg = "";
		try
		{
			msg = brandCateService.sendToSAP(brandCate);
		}catch(Exception e)
		{
			msg = BrandCommonConstant.EX_1008+e.getCause();
		}
		String navId = request.getParameter("navId");
		model.addAttribute("navId", navId);
		addMessage(redirectAttributes, msg);
		model.addAttribute("brandCate", brandCate);
		model.addAttribute("msg", msg);
		return "/biz/brand/brandcate/view";
	}
	@RequestMapping(value = "sapAll")
	public String sendSapAll(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes)  {
		String sendIds = request.getParameter("sendIds");
		Long[] ids = BrandUtils.getLongAryByStr(sendIds,";");
		String navId = request.getParameter("navId");
		String msg = "";
		try
		{
			msg = brandCateService.sendToSAP(ids);
		}catch(Exception e)
		{
			e.printStackTrace();
			msg = BrandCommonConstant.EX_1008+e.getCause();
		}
		addMessage(redirectAttributes, msg);
		return "redirect:/biz/brand/brandcate/list?navId="+navId;
	}
	
	@RequestMapping(value = "synchroniSap")
	public String synchroniSap(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes)  {
		String msg = "";
		try
		{
			msg = brandCateService.synchroniSap(new BrandCate());
		}catch(Exception e)
		{
			e.printStackTrace();
			msg = BrandCommonConstant.EX_1008+e.getCause();
		}
		addMessage(redirectAttributes, msg);
		return "redirect:/biz/brand/excel/syn";
	}
}
